package com.example.JPetStore.controller;

import com.example.JPetStore.Common;
import com.example.JPetStore.dao.CategoryDao;
import com.example.JPetStore.dao.ItemDao;
import com.example.JPetStore.dao.ProductDao;
import com.example.JPetStore.domain.Category;
import com.example.JPetStore.domain.Item;
import com.example.JPetStore.domain.Product;
import com.example.JPetStore.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@RequestMapping("/catalog")
@Controller
public class CatalogController {
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    ProductDao productDao;
    @Autowired
    ItemDao itemDao;

    @GetMapping("/MainController")
    public ModelAndView index(HttpSession session){

        ModelAndView mv=new ModelAndView();

        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;


        ArrayList<Category> categories= categoryDao.queryCategories();


        mv.addObject("categories",categories);
        mv.setViewName("/catalog/main");
        return mv;
    }

//    @WebServlet(name = "CategoryController",urlPatterns = "/jsp/catalog/CategoryController")

    @GetMapping("/CategoryController")
    public ModelAndView Category(HttpSession session, @RequestParam("cate_id") String cateId,@RequestParam("cate_name") String cateName){
//        request.setCharacterEncoding("UTF-8");
//        HttpSession session = request.getSession();
//        User user = Common.checkLogin(request, response);
//        if (user == null) return;

        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

//        String cateId = request.getParameter("cate_id");
//        String cateName = request.getParameter("cate_name");

        if(cateId==null||cateId.equals("")){
//            Common.sendErrorMessage(response,"未输入cateId！",Common.jspRoot+"catalog/Main.jsp");
            Common.sendErrorMessage(mv, "未输入cateId！", "/catalog/MainController");

//            return;
            return mv;
        }
//        ArrayList<Product> products= ProductDao.queryProducts(cateId);
        ArrayList<Product> products= productDao.queryProducts(cateId);

//        request.setAttribute("products",products);
//        request.setAttribute("cateName",cateName);
        mv.addObject("products",products);
        mv.addObject("cateName",cateName);
        System.out.println(products);
//        request.getRequestDispatcher(Common.jspRoot2+"/catalog/Category.jsp").forward(request,response);
        mv.setViewName("/catalog/category");
        return mv;
    }
    @GetMapping("/ItemController")
    public ModelAndView Item(HttpSession session,@RequestParam("item_id")String itemId){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;


        Item item=itemDao.queryItemById(itemId);

        mv.addObject("item",item);
        mv.setViewName("/catalog/item");
        return mv;
    }

    @GetMapping("/ProductController")
    public ModelAndView Product(HttpSession session,@RequestParam("pro_id")String productId,@RequestParam("pro_name")String productName){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;


        ArrayList<Item> items= itemDao.queryItems(productId);

        mv.addObject("items",items);
        mv.addObject("productName",productName);
        mv.setViewName("/catalog/product");
        return mv;
    }

    @PostMapping("/SearchItemsController")
    public ModelAndView SearchItems(HttpSession session,@RequestParam("keyword")String keyword ){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        ArrayList<Item> items=itemDao.searchItems(keyword);
        System.out.println(items);
        mv.addObject("items",items);
        mv.setViewName("catalog/searchitems");
        return mv;
    }
}
